<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use message\SendSMS;

class user extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        return $this->fetch();

    }

    public function login(Request $request)
    {
        // $request = Request::instance();
        // echo "当前模块名称是" . $request->module();
        // echo "当前控制器名称是" . $request->controller();
        // echo "当前操作名称是" . $request->action();
        $p = $request->post();
        //var_dump($_POST);
        $username = $p['username'];
        $password = md5($p['password']);
        $result = Db::name('index_users')->where('username',$username)->where('password',$password)->find();
        if($result){
            return $this->success('登录成功','/home');
        }else{
            return $this->error('登录失败');
        }
        
    }

    public function joggle()
    {
        for($i=0;$i<4;$i++){
           $ver[$i]=mt_rand(0,9);
        }
        $verify=implode($ver);
        Session::set('joggle',$verify);
        $joggle = new SendSMS();
        $res = $joggle->SendTemplate('17764508450',array($verify,5),'1');
        if ($res < 0) {
            return $this->error('注册失败(ノ°ο°)ノ高能预警 ');
        }else{
            return $this->success('注册成功',url('/indexUserIndex'));
        }
    }    
    public function reg(Request $request)
    {
        $p = $request->post();
        if($p['password1'] !== $p['password']){
            return $this->error('两次密码不一致(ノ°ο°)ノ高能预警 ', url('/adminUserList'));
        }
        $data = [
            'username' => $p['username'],
            'password' => md5($p['password']),
            'email' => $p['email']
        ];
        $result = Db::name('index_users')->data($data)->insert();
        if ($result < 0) {
            return $this->error('注册失败(ノ°ο°)ノ高能预警 ');
        }else{
            return $this->success('注册成功',url('/indexUserIndex'));
        }
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
